﻿using System;
using System.Collections.Generic;
using System.Linq;
using FirstFloor.ModernUI.Windows.Controls;
using IsikFx.Project.CommonTypes;
using IsikFx.Project.CommonTypes.Announcement;
using IsikFx.Project.CommonTypes.User;
using IsikFx.Project.CommonTypes.CRUD;
using IsikFx.Project.CommonTypes.Watchman;
using IsikFx.Project.CommonTypes.FundLog;
using log4net;
using IsikFx.Project.Utils;
using IsikFx.Project.Core;
using System.Windows.Media;
using System.Windows.Controls;
namespace IsikFx.Project.ClientApp.Helpers
{
    public static class Helper
    {
        public static readonly string USD = "USD";
        public static readonly string Sales = "Sales";
        public static readonly string Retention = "Retention";
        public static readonly string[] RetentionArray = { "FundIn", "FundOut" };
        public static readonly string[] SalesArray = { "CustomerEntry" };
        public static readonly string GoalBarFormat = ("% ") + "{0}" + ("  ") + "{1} / " + "{2}";
        public static readonly string ExceptionMessage = "Geçici süre işleminizi gerçekleştiremiyoruz, lütfen daha sonra tekrar deneyiniz.";
        public static User _user;
        public static ILog logger = LogManager.GetLogger(typeof(Helper));
        static IsikFxWcfService.ServiceClient client = new IsikFxWcfService.ServiceClient();

        public enum Roles
        {
            Undefined = 0,
            Sales = 1,
            Retention = 2,
            Admin = 3
        };

        public static string DomainName()
        {
            return Environment.UserDomainName + "_" + Environment.UserName;
        }

        public static void InitializeCurrentUser()
        {
            UserResponseMessage userResponseMsg = DataProviderFactory.GetProvider().GetCurrentUser(Helper.DomainName());

            if (!userResponseMsg.HasError)
            {
                if (userResponseMsg.Response != null && userResponseMsg.Response.User != null && userResponseMsg.Response.User.Count > 0)
                {
                    _user = userResponseMsg.Response.User[0];
                }
            }
            else
            {
                throw new Exception(userResponseMsg.ErrorMessage);
            }
        }

        public static User CurrentUser
        {
            get
            {
                return _user;
            }
        }

        public static List<Customer> GetCustomers()
        {
            List<Customer> customers = DataProviderFactory.GetProvider().GetCustomers(Convert.ToInt32(CurrentUser.UserID));
            return customers;
        }

        public static Goal GetAgentGoal()
        {
            return DataProviderFactory.GetProvider().GetAgentGoal(Convert.ToInt32(CurrentUser.UserID));
        }

        public static GoalProgress GetGoalProgress()
        {
            return DataProviderFactory.GetProvider().GetGoalProgress(Convert.ToInt32(CurrentUser.UserID));
        }

        public static AnnouncementResponseMessage GetActiveAnnouncementsAsAdmin(int adminID)
        {
            return DataProviderFactory.GetProvider().GetActiveAnnouncementsAsAdmin(adminID);
        }

        /// <summary>
        /// GetActiveAnnouncementsAsUser
        /// </summary>
        /// <param name="roleType"></param>
        /// <returns></returns>
        public static AnnouncementResponseMessage GetActiveAnnouncementsAsUser(int roleType)
        {
            return DataProviderFactory.GetProvider().GetActiveAnnouncementsAsUser(roleType);
        }

        /// <summary>
        /// InsertNewAnnouncement
        /// </summary>
        /// <param name="announcementTitle"></param>
        /// <param name="announcement"></param>
        /// <param name="roleType"></param>
        /// <returns></returns>
        public static CrudResponseMessage InsertNewAnnouncement(string announcementTitle, string announcement, int roleType)
        {
            IsikFxWcfService.ServiceClient client = new IsikFxWcfService.ServiceClient();
            string uniqeAnnouncementId = CurrentUser.UserID + "_" + CurrentUser.Name + "_" + CurrentUser.Surname;
            string createDate = DateTime.Now.ToShortDateString();
            int adminID = Convert.ToInt32(CurrentUser.UserID);
            return DataProviderFactory.GetProvider().InsertAnnouncement(adminID, uniqeAnnouncementId, announcementTitle, announcement, createDate, roleType);
        }


        /// <summary>
        /// ResetGoals
        /// </summary>
        /// <returns></returns>
        public static CrudResponseMessage ResetGoals()
        {
            return DataProviderFactory.GetProvider().ResetGoals();
        }


        /// <summary>
        /// DeleteAnnouncement
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static CrudResponseMessage DeleteAnnouncement(int id)
        {
            return DataProviderFactory.GetProvider().DeleteAnnouncement(id);
        }


        /// <summary>
        /// GetWatchmenList
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static WatchmanResponseMessage GetWatchmenList(string date)
        {
            return DataProviderFactory.GetProvider().GetWatchmenList(date);
        }


        /// <summary>
        /// UpdateWatchmenTable
        /// </summary>
        /// <param name="listWatchman"></param>
        /// <returns></returns>
        public static CrudResponseMessage UpdateWatchmenTable(List<Watchman> listWatchman)
        {
            return DataProviderFactory.GetProvider().UpdateWatchmenTable(listWatchman);
        }

        public static void ShowMessage(string message)
        {
            ModernDialog dialog = new ModernDialog();
            dialog.Content = message;
            dialog.ShowDialog();
        }

        public static FundLogResponseMessage GetMinusFunds(string userID, string fundDate)
        {
            return DataProviderFactory.GetProvider().GetMinusFundLogs(userID, fundDate);
        }

        public static FundLogResponseMessage GetPlusFunds(string userID, string fundDate)
        {
            return DataProviderFactory.GetProvider().GetPlusFundLogs(userID, fundDate);
        }

        public static FundLogResponseMessage GetAllFunds(string userID, string fundDate)
        {
            return DataProviderFactory.GetProvider().GetAllFundLogs(userID, fundDate);
        }

        public static CrudResponseMessage AddNewFundLog(string userID, string fundAmount, int isFundOut, string fundDate, string fundTime)
        {
            return DataProviderFactory.GetProvider().InsertFundLog(userID, fundAmount, isFundOut, fundDate, fundTime);
        }

        public static CrudResponseMessage SaveUserPhoto(string fileName, byte[] stream)
        {
            return DataProviderFactory.GetProvider().SaveUsersPhoto(fileName, stream);
        }

        public static void ResetAllControlsInWindow(Visual visualForm)
        {
            ChildControls ccChildren = new ChildControls();

            foreach (object o in ccChildren.GetChildren(visualForm, 5))
            {
                if (o.GetType() == typeof(TextBox))
                {
                    TextBox txt = (TextBox)o;
                    txt.Text = String.Empty;
                }
                else if (o.GetType() == typeof(DatePicker))
                {
                    DatePicker dp = (DatePicker)o;
                    dp.SelectedDate = DateTime.Today;
                }
            }
        }

        public static void InitPersonelTypeComboBox(ComboBox comboBox,bool isForAdmin = false)
        {
            List<string> list = new List<string>();
            list.Add("Lütfen Seçiniz");
            list.Add("Satış Personeli");
            list.Add("Retention Personeli");
            if (!isForAdmin)
            list.Add("Admin");

            comboBox.ItemsSource = list;
            comboBox.SelectedIndex = list.IndexOf(list.FirstOrDefault());
        }

        public static Roles GetSelectedPersonalType(ComboBox comboBox)
        {
            if (comboBox.SelectedItem.ToString().Contains("Satış"))
                return Roles.Sales;
            else if (comboBox.SelectedItem.ToString().Contains("Retention"))
                return Roles.Retention;
            else if (comboBox.SelectedItem.ToString().Contains("Admin"))
                return Roles.Admin;
            else
                return Roles.Undefined;
        }

    }
}
